package com.uwo.house.controller;

import com.uwo.house.beans.PayLogEntity;
import com.uwo.house.beans.User;
import com.uwo.house.beans.request.CustomerOrderReq;
import com.uwo.house.beans.request.DistributeOrderReq;
import com.uwo.house.beans.response.MeasureOrderResp;
import com.uwo.house.config.JWTAuthenticationToken;
import com.uwo.house.config.SmsContentConfig;
import com.uwo.house.kits.CacheKits;
import com.uwo.house.kits.SmsKits;
import com.uwo.house.service.IOrderMeasureService;
import com.uwo.house.service.IUserService;
import com.uwo.house.service.serviceImpl.AdministratorService;
import com.uwo.house.util.OrderNoUtil;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 预定量房Controller
 *
 * @author Young
 */
@Controller
@RequestMapping("/measure")
public class OrderMeasureController extends BaseController{
    @Autowired
    public IOrderMeasureService orderMeasureService;

    @Autowired
    public AdministratorService administratorService;

    @Autowired
    public IUserService userService;

    /**
     * 保存预约量房信息
     *
     * @param customerOrderReq
     * @return
     */
    @RequestMapping(value = "/order", method = RequestMethod.POST)
    @ResponseBody
    public Map orderMeasure(@RequestBody CustomerOrderReq customerOrderReq,
                            @RequestHeader(value = "Authorization", required = false) String token) {
        String[] strs = token.split(" ");
        token = (strs.length == 2) ? strs[1] : null;
        JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) CacheKits.get(token);
        String userId = jwtToken.getUser().getId();
        customerOrderReq.setOrderId(OrderNoUtil.getOrderIdByUUId());
        customerOrderReq.setDeleteFlag("0");
        customerOrderReq.setOrderStatus(1);
        customerOrderReq.setUserId(userId);
        int result = this.orderMeasureService.addOrderMeasure(customerOrderReq);
        BigDecimal payShouldPrice = this.administratorService.getDepositMoney();//应付金额
        PayLogEntity payLogEntity = new PayLogEntity();
        payLogEntity.setId(UUID.randomUUID().toString().replaceAll("-", ""));//主键id
        payLogEntity.setOrderId(customerOrderReq.getOrderId());//订单编号
        payLogEntity.setPayerId(userId);//付款人id
        payLogEntity.setPayShouldPrice(payShouldPrice);
        this.orderMeasureService.addPayLog(payLogEntity);//添加付款日志
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, customerOrderReq.getOrderId());
        }
        return UtilReturn.resErrorMap("预约失败", UtilReturn.ERROR_CODE);
    }

    /**
     * 根据packageId-获取装修风格
     *
     * @param packageId
     * @return
     */
    @RequestMapping(value = "/package", method = RequestMethod.GET)
    @ResponseBody
    public Map orderMeasureByPackage(@RequestParam("packageId") String packageId) {
        String decorationStyle = this.orderMeasureService.getDecorationById(packageId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, decorationStyle);
    }


    /**
     * 管理员中心-查询预约订单信息
     *
     * @param orderId
     * @param orderDateFromStr
     * @param orderDateToStr
     * @param orderSts
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getMeasureOrder(@RequestParam(value = "orderId", required = false) String orderId,
                               @RequestParam(value = "orderDateFromStr", required = false) String orderDateFromStr,
                               @RequestParam(value = "orderDateToStr", required = false) String orderDateToStr,
                               @RequestParam(value = "orderSts") int orderSts) {
        Date orderDateFrom = com.uwo.house.util.DateUtils.getDate(orderDateFromStr);
        Date orderDateTo = com.uwo.house.util.DateUtils.getDate(orderDateToStr);
        List<MeasureOrderResp> list = this.orderMeasureService.getMeasureOrder(orderId, orderDateFrom, orderDateTo, orderSts);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 管理员中心-预约测量-详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    @ResponseBody
    public Map getMeasureDetailsByOrderId(@RequestParam("orderId") String orderId) {
        MeasureOrderResp measureOrderResp = this.orderMeasureService.getMeasureDetailsByOrderId(orderId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, measureOrderResp);
    }

    /**
     * 管理员中心-预约测量-删除
     *
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteMeasureOrder(@RequestParam(value = "orderId") String orderId) {
        int result = this.orderMeasureService.deleteMeasureOrder(orderId);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败",UtilReturn.ERROR_CODE);
    }

    /**
     * 管理员中心-预约测量-派单
     *
     * @param distributeOrderReq
     * @return
     */
    @RequestMapping(value = "/distribute", method = RequestMethod.POST)
    @ResponseBody
    public Map distributeOrder(@RequestBody DistributeOrderReq distributeOrderReq) {
        int result = this.orderMeasureService.distributeOrder(distributeOrderReq.getMeasureStaff(), distributeOrderReq.getOrderId());
        if (result == 1) {
            try {
                User user = userService.selectUserById(distributeOrderReq.getMeasureStaff());
                String mobile = user.getMobile();//测量人员手机号
                SmsKits.send(SmsContentConfig.distributeContent, mobile);//发送短信提醒
            } catch (Exception e) {
                System.out.print("短信发送出错");
            }
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "派单成功");
        }
        return UtilReturn.resErrorMap("派单失败", UtilReturn.ERROR_CODE);
    }
}
